﻿using System;
using System.Windows.Forms;
using DeviceManagement;

namespace DeviceNotificationSample1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            InitializeDeviceNotifications();
        }

        private void InitializeDeviceNotifications()
        {
            var notificationHandler = new VolumeNotificationHandler();

            notificationHandler.VolumeArrival += new EventHandler<DeviceNotificationEventArgs>(notificationHandler_VolumeArrival);
            notificationHandler.VolumeRemoveComplete += new EventHandler<DeviceNotificationEventArgs>(notificationHandler_VolumeRemoveComplete);

            var deviceNotification = new DeviceNotification() { NotificationHandler = notificationHandler };

            deviceNotification.Register(DeviceType.LogicalVolume);

            this.components.Add(deviceNotification);
        }


        void notificationHandler_VolumeRemoveComplete(object sender, DeviceNotificationEventArgs e)
        {
            Volume volume = e.GetVolume();

            MessageBox.Show(string.Format("Drive {0}: was removed.", volume.DriveLetter));
        }

        void notificationHandler_VolumeArrival(object sender, DeviceNotificationEventArgs e)
        {
            Volume volume = e.GetVolume();

            MessageBox.Show(string.Format("Drive {0}: was successfully added.", volume.DriveLetter));
        }
    }
}